﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceMan.Entities.Attachments
{
    public class IC_ATT_Attachment
    {
        protected Vector2 Position, Velocity;
        protected float Rotation;
        protected Dictionary<string, IC_ATT_Attachment> Children = new Dictionary<string, IC_ATT_Attachment>();
        protected Dictionary<string, Vector2> AttachmentPoints;
        protected C_Drawing DrawOperation;
        public void BaseInitialize(Dictionary<string, Vector2> AttachmentPoints)
        {
            this.AttachmentPoints = AttachmentPoints;
        }

        public void Disable()
        {
            if (this is C_ATT_StandardSpeaker)
            {
                C_ATT_StandardSpeaker Speaker = (C_ATT_StandardSpeaker)this;
                Speaker.StopSound();
            }
            if (Children != null)
            {
                foreach (IC_ATT_Attachment Child in Children.Values)
                {
                    Child.Disable();
                }
                this.Children = null;
            }
            
        }


        virtual public void Update(Vector2 Position,Vector2 Velocity, float Rotation)
        {
            this.Position = Position;
            this.Rotation = Rotation;
            this.Velocity = Velocity;
            if (AttachmentPoints != null)
            {
                for (int i = 0; i < AttachmentPoints.Count; i++)
                {
                    if (Children.ContainsKey(AttachmentPoints.ElementAt(i).Key))
                    {
                        Children[AttachmentPoints.ElementAt(i).Key].Update(S_Global.getRotPos(AttachmentPoints[AttachmentPoints.ElementAt(i).Key], Position, Rotation) + Position, Velocity,Rotation);
                    }
                }
            }

            if (this is I_AttachmentWeapon)
            {
                I_AttachmentWeapon weapon = (I_AttachmentWeapon)this;
                weapon.Update();
            }

            if (this is I_Effect)
            {
                I_Effect effect = (I_Effect)this;
                effect.Update();
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch, DrawMode drawMode, Rectangle DrawSpace)
        {
            if(Children != null)
            {
                foreach (IC_ATT_Attachment child in Children.Values)
                {
                    child.Draw(spriteBatch, drawMode, DrawSpace);
                }

            }
            if (DrawOperation != null)
                DrawOperation.Draw(spriteBatch, drawMode, DrawSpace);
        }

        public void AddAttachment(string AttachmentPoint, IC_ATT_Attachment Attachment)
        {
            if (AttachmentPoints != null)
            {
                if (AttachmentPoints.ContainsKey(AttachmentPoint))
                {
                    if (!Children.ContainsKey(AttachmentPoint))
                    {
                        Children.Add(AttachmentPoint, Attachment);
                    }
                }
            }
        }

        public void RemoveAttachment(string AttachmentPoint)
        {
            if (AttachmentPoint.ToUpper() == "ALL")
            {
                Children.Clear();
            }

            else
            {
                if (Children.ContainsKey(AttachmentPoint))
                {
                    Children.Remove(AttachmentPoint);
                }
            }
        }
    }
}
